import { getToken } from './index'

function navigateTo(options) {
  if (beforeEach(options.url)) {
    uni.navigateTo(options);
  }
}
 
// 封装 redirectTo
function redirectTo(options) {
  if (beforeEach(options.url)) {
    uni.redirectTo(options);
  }
}
 
// 封装 switchTab
function switchTab(options) {
  if (beforeEach(options.url)) {
    uni.switchTab(options);
  }
}
 
// 封装 reLaunch
function reLaunch(options) {
  if (beforeEach(options.url)) {
    uni.reLaunch(options);
  }
}
 
// 封装 navigateBack
function navigateBack(options) {
  if (beforeEach()) {
    uni.navigateBack(options);
  }
}

const routes = [
  'pages/Home/index',
]

function beforeEach(url){
  let isLoginUrl = false
  routes.map(item=>{
    if(url.includes(item) !== -1){
      isLoginUrl = true
    }
  })
  if(isLoginUrl){
    const token = getToken()
    if(!token){
      uni.showToast({
        title: '请先登录',
      })
      uni.navigateTo({
        url: '/pages/Login/index'
      })
      return false
    }else{
      return true
    }
  }
  return true
}

export default {
  navigateTo,
  redirectTo,
  switchTab,
  reLaunch,
  navigateBack
}
